﻿package frocessing.color
{

    public class ColorHSV extends AbstractColorUpdater implements IColor
    {
        private var _h:Number;
        private var _s:Number;
        private var _v:Number;

        public function ColorHSV(param1:Number = 0, param2:Number = 1, param3:Number = 1, param4:Number = 1)
        {
            this.hsv(param1, param2, param3);
            _a = param4;
            return;
        }// end function

        public function toString() : String
        {
            return "[HSV(" + this._h.toFixed(2) + "," + this._s.toFixed(2) + "," + this._v.toFixed(2) + ")A(" + _a.toFixed(2) + ")]";
        }// end function

        override protected function apply_grayscale() : void
        {
            this._s = 0;
            this._v = _r / 255;
            return;
        }// end function

        public function set s(param1:Number) : void
        {
            if (param1 > 1)
            {
                this._s = 1;
            }
            else if (param1 < 0)
            {
                this._s = 0;
            }
            else
            {
                this._s = param1;
            }
            check_rgb_flg = true;
            return;
        }// end function

        public function get hr() : Number
        {
            return Math.PI * this._h / 180;
        }// end function

        public function hsv(param1:Number, param2:Number = 1, param3:Number = 1) : void
        {
            this._h = param1;
            if (param2 > 1)
            {
                this._s = 1;
            }
            else if (param2 < 0)
            {
                this._s = 0;
            }
            else
            {
                this._s = param2;
            }
            if (param3 > 1)
            {
                this._v = 1;
            }
            else if (param3 < 0)
            {
                this._v = 0;
            }
            else
            {
                this._v = param3;
            }
            check_rgb_flg = true;
            return;
        }// end function

        public function set v(param1:Number) : void
        {
            if (param1 > 1)
            {
                this._v = 1;
            }
            else if (param1 < 0)
            {
                this._v = 0;
            }
            else
            {
                this._v = param1;
            }
            check_rgb_flg = true;
            return;
        }// end function

        public function get h() : Number
        {
            return this._h;
        }// end function

        public function set h(param1:Number) : void
        {
            this._h = param1;
            check_rgb_flg = true;
            return;
        }// end function

        public function get v() : Number
        {
            return this._v;
        }// end function

        public function get s() : Number
        {
            return this._s;
        }// end function

        public function set hr(param1:Number) : void
        {
            this._h = 180 * param1 / Math.PI;
            check_rgb_flg = true;
            return;
        }// end function

        override protected function apply_rgb() : void
        {
            if (_r != _g || _r != _b)
            {
                if (_g > _b)
                {
                    if (_r > _g)
                    {
                        this._v = _r / 255;
                        this._s = (_r - _b) / _r;
                        this._h = 60 * (_g - _b) / (_r - _b);
                    }
                    else if (_r < _b)
                    {
                        this._v = _g / 255;
                        this._s = (_g - _r) / _g;
                        this._h = 60 * (_b - _r) / (_g - _r) + 120;
                    }
                    else
                    {
                        this._v = _g / 255;
                        this._s = (_g - _b) / _g;
                        this._h = 60 * (_b - _r) / (_g - _b) + 120;
                    }
                }
                else if (_r > _b)
                {
                    this._v = _r / 255;
                    this._s = (_r - _g) / _r;
                    this._h = 60 * (_g - _b) / (_r - _g);
                    if (this._h < 0)
                    {
                        this._h = this._h + 360;
                    }
                }
                else if (_r < _g)
                {
                    this._v = _b / 255;
                    this._s = (_b - _r) / _b;
                    this._h = 60 * (_r - _g) / (_b - _r) + 240;
                }
                else
                {
                    this._v = _b / 255;
                    this._s = (_b - _g) / _b;
                    this._h = 60 * (_r - _g) / (_b - _g) + 240;
                }
            }
            else
            {
                var _loc_1:int = 0;
                this._s = 0;
                this._h = _loc_1;
                this._v = _r / 255;
            }
            return;
        }// end function

        public function clone() : ColorHSV
        {
            var _loc_1:* = new ColorHSV(this._h, this._s, this._v, _a);
            _loc_1.copyFrom(this);
            return _loc_1;
        }// end function

        override protected function update_rgb() : void
        {
            var _loc_1:Number = NaN;
            if (this._s > 0)
            {
                _loc_1 = (this._h < 0 ? (this._h % 360 + 360) : (this._h % 360)) / 60;
                if (_loc_1 < 1)
                {
                    _r = Math.round(255 * this._v);
                    _g = Math.round(255 * this._v * (1 - this._s * (1 - _loc_1)));
                    _b = Math.round(255 * this._v * (1 - this._s));
                }
                else if (_loc_1 < 2)
                {
                    _r = Math.round(255 * this._v * (1 - this._s * (_loc_1 - 1)));
                    _g = Math.round(255 * this._v);
                    _b = Math.round(255 * this._v * (1 - this._s));
                }
                else if (_loc_1 < 3)
                {
                    _r = Math.round(255 * this._v * (1 - this._s));
                    _g = Math.round(255 * this._v);
                    _b = Math.round(255 * this._v * (1 - this._s * (3 - _loc_1)));
                }
                else if (_loc_1 < 4)
                {
                    _r = Math.round(255 * this._v * (1 - this._s));
                    _g = Math.round(255 * this._v * (1 - this._s * (_loc_1 - 3)));
                    _b = Math.round(255 * this._v);
                }
                else if (_loc_1 < 5)
                {
                    _r = Math.round(255 * this._v * (1 - this._s * (5 - _loc_1)));
                    _g = Math.round(255 * this._v * (1 - this._s));
                    _b = Math.round(255 * this._v);
                }
                else
                {
                    _r = Math.round(255 * this._v);
                    _g = Math.round(255 * this._v * (1 - this._s));
                    _b = Math.round(255 * this._v * (1 - this._s * (_loc_1 - 5)));
                }
            }
            else
            {
                var _loc_2:* = Math.round(255 * this._v);
                _b = Math.round(255 * this._v);
                var _loc_2:* = _loc_2;
                _g = _loc_2;
                _r = _loc_2;
            }
            check_rgb_flg = false;
            return;
        }// end function

    }
}
